<?php

/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class OController extends CController {

    /**
     * @var string the default layout for the controller view. Defaults to 'column1',
     * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
     */
//	public $layout='column1';
    //public $layout = 'dashBoardLayout';
    public $layout='mainLayout';

    /**
     * @var array context menu items. This property will be assigned to {@link CMenu::items}.
     */
    public $menu = array();

    /**
     * @var array the breadcrumbs of the current page. The value of this property will
     * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
     * for more details on how to specify this property.
     */
    public $breadcrumbs = array();
    public $defaultAction = 'admin';
    public $assetsUrl;
    public $imagesPath;
    public $cssPath;
    public $jsPath;
    public $pageHeader;
    public $debugMode = false;

    /**
     * @var string Current country code
     */
    public $countryCode;
    public $currency;
    public $logoutUrl;

    /**
     * @var string current working form id
     */
    public $formId = 'form-id';

    public function init() {
        parent::init();
        Yii::app()->setTheme('admin');
        
        //$this->assetsUrl = $this->module->assetsUrl();        
        $this->imagesPath = Yii::app()->theme->baseUrl . '/img';
        $this->cssPath = Yii::app()->theme->baseUrl . '/css';
        $this->jsPath = Yii::app()->theme->baseUrl . '/js';

        $this->debugMode = Yii::app()->params['debugMode'];
        $this->logoutUrl = Yii::app()->createUrl('user/auth/logout', array('token' => Yii::app()->request->csrfToken));
        //Yii::app()->language = 'zh_cn';
        $this->countryCode = Environment::isProduction() ? Yii::app()->geoip->lookupCountryCode() : Yii::app()->params['defaultCountryCode'];
        $this->currency = Yii::app()->params['defaultCurrency'];
        
        Yii::setPathOfAlias('componentsViewPath', $this->module->viewPath . '/widgets');

        Lang::setModule($this->module->name);

        $this->pageTitle = CHtml::encode(Lang::t('app.title', array('{name}' => Yii::app()->name)));
    }

    public function filters() {
        return array(
            'accessControl',
        );
    }

    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === $this->formId) {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

    public function setFlash($type, $message) {
        Yii::app()->user->setFlash($type, $message);
    }
    
    public function getFlashes($htmlOptions=array(), $tag='div'){
        foreach (Yii::app()->user->getFlashes() as $key => $message) {
            if(isset($htmlOptions['class'])) $htmlOptions['class']=str_replace('{status}',$key, $htmlOptions['class']);
            echo CHtml::openTag($tag, $htmlOptions)
            . $message
            . CHtml::closeTag($tag);
            echo "\n";
        }//end foreach loop
    }

    protected function beforeRender($view) {
        if (parent::beforeRender($view)) {
            $this->breadcrumbs=  array_unique(array_filter($this->breadcrumbs));
        }//end if	    
        return true;
    }
    
    public function render($view, $data = null, $return = false, $options = null) {
        $output = parent::render($view, $data, true);
        if (!YII_DEBUG) {
            $compactor = Yii::app()->contentCompactor;
            if ($compactor == null)
                throw new CHttpException(500, Yii::t('messages', 'Missing component ContentCompactor in configuration.'));

            $output = $compactor->compact($output, $options);
        }
        if ($return)
            return $output;
        else
            echo $output;
    }

}//end class